package com.zyj.test.month1.day11;

import java.util.Deque;
import java.util.LinkedList;

/**
 * @author Yijia Zhang
 * @create 2024-05-18 11:49
 */
public class code0020 {
    public boolean isValid(String s) {
        Deque<Character> stack = new LinkedList<>();
        char[] cs = s.toCharArray();
        for (int i = 0; i < cs.length; i++) {
            char c = cs[i];
            switch (c) {
                case '(':
                case '[':
                case '{':
                    stack.push(c);
                    break;
                case ')':
                    if (stack.isEmpty() || stack.pop() != '(') {
                        return false;
                    }
                    break;
                case '}':
                    if (stack.isEmpty() || stack.pop() != '{') {
                        return false;
                    }
                    break;
                case ']':
                    if (stack.isEmpty() || stack.pop() != '[') {
                        return false;
                    }
                    break;
            }
        }
        return stack.isEmpty();
    }
}
